﻿var structtbmanage = {
    gridDefine: {
        columns: [
            { display: lang.table_ID, name: 'table_id', minWidth: 60 },
            { display: lang.table_name, name: 'table_name', minWidth: 200 },
            { display: lang.table_code, name: 'table_code', minWidth: 80 },
            { display: lang.Remarks, name: 'desctription', minWidth: 200 },
            { display: lang.export_permission, name: 'itemname', minWidth: 200, render: LinkAuthority, align: 'center' }
//            { display: lang.operation, name: 'table_id2', minWidth: 100, render: LinkOP, align: 'center' }
        ],
        url: App_Config.TadongUrl + "/AjaxMappingHandler.aspx",
        urlType: "QueryDictTableList"
    },
    init: function () {
        jsPageRender(this, true);
        getAuthority();
    },
    search: function () {
        jsPageSearch(this);
    }
};

var authorityArr=[];
function getAuthority(){
    $.ajaxHandler({
        url: App_Config.TadongUrl + "/AjaxMappingHandler.aspx",
        urlType: "QueryDictItemList2",
        model: function () { return 999;},
        success: function (data) {
            if (data) {
                authorityArr=data;
            }
        }
    });
}

function LinkOP() {
    return '';
}

function LinkAuthority(value, row, index) {
    if (lang.SysLanguage == "en") {
        var str = row.itemdesc || '';
    }
    else {
        var str = row.itemname || '';
    }
    return str + '<span id="op_' + row.table_id + '"><a href="javascript:void(0)" onclick="handlerlevel(\'' + row.table_id + '\',\'' + row.itemvalue + '\');">['+lang.set+']</a></span>';
}

function handlerlevel(id,value) {
    var html = [];
    html.push('<select>');
    for (var i = 0; i < authorityArr.length; i++) {
        if (lang.SysLanguage == "en") {
            html.push('<option value="' + authorityArr[i].ITEMVALUE + '">' + authorityArr[i].ITEMDESC + '</option>');
        }
        else {
            html.push('<option value="' + authorityArr[i].ITEMVALUE + '">' + authorityArr[i].ITEMNAME + '</option>');
        }
    }
    html.push('<select>');
    html.push('<input type="button" value='+lang.save+' onclick="saveAuthority('+id+')" >');
    $('#op_' + id).html(html.join(''));
}

function saveAuthority(id) {
    $.ajaxHandler({
        url: App_Config.TadongUrl + "/AjaxMappingHandler.aspx",
        urlType: "SaveTableAuthority",
        model: function () { return $.toJSON({ tableid: id, authority: $('#op_' + id + ' select').val() }); },
        success: function (data) {
            if (data) {
                //structtbmanage.search();
            }
        }
    });
}